package com.wafersystems.springbootdemo.controller;

import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.tenant.TenantContextHolder;
import com.pig4cloud.pigx.common.security.feign.RemoteUserService;
import com.wafersystems.springbootdemo.entity.AppInfoProperties;
import com.wafersystems.springbootdemo.feign.RemoteAdminService;
import com.wafersystems.springbootdemo.task.DemoAsyncTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器
 * @author tandk
 */
@Slf4j
@Controller
@RefreshScope
public class TestController {

    @Autowired
    RemoteUserService userService;

    @Autowired
    RemoteAdminService adminService;

    /**
     * test Hello World
     * @return
     */
    @RequestMapping("test")
    @ResponseBody
    public Object test(){
        return adminService.feign("张三", SecurityConstants.FROM_IN);
    }

    /**
     * 测试返回jsp web
     * @return
     */
    @RequestMapping("/testJsp")
    public ModelAndView testJsp(){
        Map<String, String> retMap = new HashMap<>();
        retMap.put("name", "威发");
        return new ModelAndView("testJsp", retMap);
    }

    /**
     * 测试跳转web
     * @return
     */
    @RequestMapping("/testRedirect")
    public ModelAndView testRedirect(){
        return new ModelAndView("redirect:http://www.baidu.com");
    }

    /**
     * 测试返回对象
     * @return
     */
    @RequestMapping("/testReturn")
    @ResponseBody
    public Object testReturn(){
        Map map = new HashMap(2);
        map.put("status", 1);
        map.put("message", "success");
        return map;
    }

    /**
     * 第一种方式：使用`@Value`获取配置文件参数值
     */
    @Value("${aaa.bbb}")
    private String aaaBbb;

    /**
     * 测试获取yml配置文件参数值
     * @return
     */
    @RequestMapping("/testYml")
    @ResponseBody
    public String testYml(){
        return aaaBbb;
    }


    /**
     * 第二种方式：使用`@ConfigurationProperties`获取配置文件参数值
     */
    @Autowired
    AppInfoProperties appInfoProperties;

    /**
     * 测试获取属性配置
     * @return
     */
    @RequestMapping("/testProperties")
    @ResponseBody
    public Object testProperties(){
        return appInfoProperties;
    }

    /**
     * 第三种方式：使用`Environment`获取配置文件参数值
     */
    @Autowired
    private Environment env;

    /**
     * 测试获取属性配置
     * @return
     */
    @RequestMapping("/testEnv")
    @ResponseBody
    public Object testEnv(){
        return env.getProperty("appinfo.name");
    }

    /**
     * 注入restTemplate
     */
    @Autowired
    RestTemplate restTemplate;

    /**
     * 测试restTemplate使用
     * @return
     */
    @RequestMapping("/testRestTemplate")
    @ResponseBody
    public String testRestTemplate(){
        String url = "http://localhost:8888/testReturn";
        String s = restTemplate.getForObject(url, String.class);
        return s;
    }

    /**
     * 注入redisTemplate
     */
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 测试redisTemplate使用
     * @return
     */
    @RequestMapping("/testRedisTemplate")
    @ResponseBody
    public String testRedisTemplate(){
        String key = "testRedisTemplate";
        redisTemplate.boundValueOps(key).set("hello world");
        return redisTemplate.boundValueOps(key).get().toString();
    }

    /**
     * 注入异步任务service
     */
    @Autowired
    DemoAsyncTask demoAsyncTask;

    /**
     * 测试异步任务
     * @return
     */
    @RequestMapping("/testAsyncTask")
    @ResponseBody
    public String testAsyncTask(){
        log.info("请求接口/testRedisTemplate 开始");
        demoAsyncTask.asyncTask();
        log.info("请求接口/testRedisTemplate 结束");
        return "ok";
    }

    /**
     * 测试返回异常
     * @return
     */
    @RequestMapping("/testEx")
    @ResponseBody
    public Object testEx(){
        throw new NullPointerException("空指针异常");
    }
}
